﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;

/// <summary>
///Template 的摘要说明
/// </summary>


    public enum TempType
    { Index, Class, News, Special, ChIndex, ChClass, ChNews, Chspecial }
    public class Template
    {   /// <summary>
        /// 当前新闻ID
        /// </summary>
        protected string _newsid = null;
        /// <summary>
        /// 模板的物理路径和名称
        /// </summary>
        protected string _temppath;
        /// <summary>
        /// 模板类型
        /// </summary>
        protected TempType _temptype;
        /// <summary>
        /// 模板的文件内容
        /// </summary>
        protected string _tempcontent = string.Empty;
        /// <summary>
        /// 模板的最终的内容
        /// </summary>
        protected string _tempfinallyconent = string.Empty;

        protected string _flag = null;

        /// <summary>
        /// 构造函数
        /// </summary> 
        /// <param name="temppath">模板物理路径</param>
        /// <param name="TmptType">模板类型</param>
        public Template(string temppath, TempType temptype)
        {
            _temppath = temppath;
            _temptype = temptype;
        }
        /// <summary>
        /// 设置或获取新闻编号
        /// </summary>
        public string NewsID
        {
            set { _newsid = value; }
            get { return _newsid; }
        }
        /// <summary> 
        /// 转换模板所有的标签
        /// </summary>
        public void ReplaceIMLabels()
        {
            string pattern = @"\[BMC:unLoop,[^\]]+\][\s\S]*?\[/BMC:unLoop\]|\[BMC:Loop,[^\]]+\][\s\S]*?\[/BMC:Loop\]";
            Regex reg = new Regex(pattern, RegexOptions.Compiled);

            Match m = reg.Match(_tempfinallyconent);
            while (m.Success)
            {
                string content = m.Value;
                LabelMass lm = new LabelMass(content, this._newsid, this._flag);
                lm.TemplateType = this.MyTempType;
                lm.ParseContent();
                string s = lm.Parse();
                _tempfinallyconent = _tempfinallyconent.Replace(content, s);
                m = reg.Match(_tempfinallyconent);
            }
        }

        /// <summary> 
        /// 转换模板所有的标签(从数据库中读取)
        /// </summary>
        public void ReplaceLabels()
        {
            string pattern = @"\{BMC[^\}]+\}";
            GetHTML();
            _tempfinallyconent = _tempcontent;
        }
        /// <summary>
        /// 读取模板的内容到变量
        /// </summary>
        public void GetHTML()
        {
            _tempcontent = ReadHtml(_temppath);
        }
        /// <summary>
        /// 获取模板的路径
        /// </summary>
        public string TempFilePath
        {
            get { return _temppath; }
        }
        /// <summary>
        /// 获取模板的内容
        /// </summary>
        public string OriginalContent
        {
            get { return _tempcontent; }
        }
        /// <summary>
        /// 获取模板转换标签后的内容
        /// </summary>
        public string FinallyContent
        {
            get { return _tempfinallyconent; }
            set { _tempfinallyconent = value; }
        }
        /// <summary>
        /// 获取当前模板的类型
        /// </summary>
        public TempType MyTempType
        {
            get { return _temptype; }
        }
        /// <summary>
        /// 如果两个模板的路径相同则返回true
        /// </summary>
        /// <param name="t1">比较的模板</param>
        /// <param name="t2">比较的模板</param>
        /// <returns></returns>
        public static bool operator ==(Template t1, Template t2)
        {
            if (t1.TempFilePath.Equals(t2.TempFilePath))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 如果两个模板的路径不相同则返回true
        /// </summary>
        /// <param name="l1">比较的模板</param>
        /// <param name="l2">比较的模板</param>
        /// <returns></returns>
        public static bool operator !=(Template t1, Template t2)
        {
            if (t1.TempFilePath.Equals(t2.TempFilePath))
                return false;
            else
                return true;
        }
        /// <summary>
        /// 重写方法
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() * 2;
        }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public string ReadHtml(string Path)
        {
            string result = string.Empty;
            if (File.Exists(Path))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(Path))
                    {
                        result = sr.ReadToEnd();
                    }
                }
                catch
                { }
            }
            else
            {
                result = "模板不存在!";
            }
            return result;
        }
    }
    
